﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ALMKickstarter.Infrastructure;
using ALMKickstarter.Models;

namespace ALMKickstarter.Controllers
{
    public class ProjectController : ALMKickstarterController
    {
		private ProjectRepository repository;

		public ProjectController()
		{
			this.repository = new ProjectRepository();
		}


        //
        // GET: /Project/
        public ActionResult Index()
        {
            return View( repository.GetProjects() );
        }

        //
        // GET: /Project/Details/5
        public ActionResult Details(int id)
        {
            return View(repository.GetProjectById(id));
        }

        //
        // GET: /Project/Create
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Project/Create
        [HttpPost]
        public ActionResult Create(ProjectModel project)
        {
            try
            {
				repository.InsertProject(project);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Project/Edit/5
        public ActionResult Edit(int id)
        {
            return View(repository.GetProjectById(id));
        }

        //
        // POST: /Project/Edit/5
        [HttpPost]
        public ActionResult Edit(ProjectModel project)
        {
            try
            {
				repository.UpdateProject(project);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Project/Delete/5
        public ActionResult Delete(int id)
        {
            return View(repository.GetProjectById(id));
        }

        //
        // POST: /Project/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
				repository.DeleteProject(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
